// pages/mine/mine.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1, //默认当前页数
    totalPage: '', //总条数
    /*轮播图 配置*/
    imgUrls: [],
    indicatorDots: true, // 是否显示面板指示点
    autoplay: true, // 是否自动切换
    interval: 5000, // 自动切换时间间隔
    duration: 500, // 滑动动画时长
    circular: true, // 是否采用衔接滑动
    swiperCurrent: 0,
    winHeight: "", //窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    menus: [], //菜单列表
    newsList: [], //资讯列表
    loading:false,
    loadingComplete:false,
    loadingHidden:true,//加载动画
    noNews:true,
    menuId:"",
    menuName:""
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    console.log(e.detail.current)
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
    this.data.menuId = this.data.menus[cur].menuID;
    this.data.menuName = this.data.menus[cur].menuName;
    this.getNewsList(this.data.menus[cur].menuID, true)
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // 高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    //资讯菜单
    wx.request({
      url: getApp().globalData.myHttp + '/news/getAllMenus',
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.data.success) {
          that.setData({
            menus: res.data.obj
          })
          that.getNewsList(res.data.obj[0].menuID, true);
        }
      },
      complete: (res) => {
        if (res.statusCode != 200) {
          that.setData({
            noNews: false
          })
        }
      }
    });
    //轮播图
    wx.request({
      url: getApp().globalData.myHttp + '/rotarymap/getListByModule?module=0&homeModule=0&size=4',
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.data.success) {
          that.setData({
            imgUrls: res.data.obj.records
          })
        }
      }
    })
  },
  //资讯列表
  getNewsList: function (menuId, isPage) {
    var that = this
    if (isPage === true) {
      that.setData({
        page: 1,
        newsList: [],
        loading:false,
        loadingComplete:false,
        loadingHidden:false
      })
    } else {
      var num = that.data.page + 1
      if (num <= that.data.totalPage) {
        that.setData({
          page: num
        })
      } else if (num > that.data.totalPage) {
        that.setData({
          loading: false,
          loadingComplete:true
        })
        return;
      }
    }
    wx.request({
      url: getApp().globalData.myHttp + '/news/getArticleByMenuId?MenuID=' + menuId + "&current=" + that.data.page + "&size=5",
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.data.success) {
          var records = that.splitArray(res.data.obj.records, that.data.newsList)
          for (let i = 0; i < records.length; i++) {
            var imgUrl = records[i].imgurl
            if (imgUrl.indexOf('http')) {
              records[i].imgurl = getApp().globalData.imgUrlHttp + "/upload/" + imgUrl
            }
          }
          that.setData({
            newsList: records,
            totalPage: res.data.obj.pages,
            loadingHidden:true
          })
        }
      }
    })
  },
  //图片加载失败
  errFunction: function (e) {
    if (e.type = 'error') {
      var index = e.currentTarget.dataset.index
      var imglist = this.data.newsList
      imglist[index].imgurl = '/image/noPic.png'
      this.setData({
        newsList: imglist
      })
    }
  },
  //查看详情
  newDetail: function (e) {
    var index = e.currentTarget.dataset.index
    var news = this.data.newsList[index]
    var artCss = news.artCss
    if(artCss == '1'){
      wx.navigateTo({
        url: '../newsdetail/newsdetail?news=' + encodeURIComponent(JSON.stringify(news))
      })
    }else if(artCss == '6'){
      if(this.data.menuId == '5' || this.data.menuName == '健康视频'){
        wx.navigateTo({
          url: '../healthVideo/healthVideo?news=' + encodeURIComponent(JSON.stringify(news))
        })
      }else{
        wx.navigateTo({
          url: '../daily/daily?url=' + encodeURIComponent(news.contents)
        })
      }
    }
    
  },
  //轮播图跳转
  toDetail: function (e) {
    var index = e.currentTarget.dataset.index
    var img = this.data.imgUrls[index]
    var type = img.type
    // type 0是内链 1是外链
    if (type == '0') {
      wx.navigateTo({
        url: '../newsdetail/newsdetail?artId=' + img.url
      })
    } else {
      wx.navigateTo({
        url: '../daily/daily?url=' + encodeURIComponent(img.url)
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 标题栏显示刷新图标，转圈圈
    wx.showNavigationBarLoading()
    //请求数据
    this.getNewsList(this.data.menus[this.data.currentTab].menuID, true)
    setTimeout(() => {
      // 标题栏隐藏刷新转圈圈图标
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh();
    }, );
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if(!that.data.loadingComplete){
      that.setData({
        loading: true
      })
    }
    setTimeout(() => {
      this.getNewsList(this.data.menus[this.data.currentTab].menuID)
    }, 2000);
  },

  /**
   * 合并数组
   */
  splitArray: function (list, sp) {
    if (typeof list != 'object') return [];
    if (sp === undefined) sp = [];
    for (var i = 0; i < list.length; i++) {
      sp.push(list[i]);
    }
    return sp;
  },


  //轮播图的切换事件 
  swiperChange: function (e) {
    //只要把切换后当前的index传给<swiper>组件的current属性即可 
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  //点击指示点切换 
  chuangEvent: function (e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  }
})